<?php

namespace Common\Model;

use Think\Model;

class OrderModel extends Model {

    /**
     *  获取单个订单
     */
    public function getOrder($id = null, $where = array()) {
        if (!empty($id)) {
            $where['orderID'] = $id;
        }

        $result = $this->where($where)->find();

        return $result;
    }

    /**
     * 获得列表（带基本查询）
     */
    public function getAllOrderList($where = array(), $order = array()) {
        $pre = C("DB_PREFIX");
        $sql1 = "select  sum(num)   from $pre" . "order_goods as og where og.orderID = o.orderID";
        $rt = M("Order o")
                ->field("o.orderID,o.orderType,o.userID,o.orderSn,o.province,o.city,"
                        . "o.district,o.address,o.consignee,o.phone,o.zcode,"
                        . "o.logisticsNo,o.logisticsStatus,o.totalPrice,o.payPrice,o.status,"
                        . "o.isComment,o.payModeID,o.payModeName,o.payTime,o.addTime,($sql1) as numOfPack,"
                        . "u.phone userPhone,u.account userAccount,pl.paygatewayOrderNo liushui")
                ->join("LEFT JOIN  $pre" . "user u on o.userID = u.userID LEFT JOIN $pre" . "pay_log pl on o.orderSn = pl.orderNo and pl.isError = 0")
                ->where($where)->order($order)->select(); 
        if($rt) {
            D("OrderGoods")->_getGoodsForOrder($rt);
        }

        foreach($rt AS $key=>$value){
            $nums = "";
            $result = M("OrderGoods")->where(array('orderID'=>$value['orderID']))->select();
            if(count($result)>1){
                foreach($result AS $k=>$v){
                    if(end($result)!= $v){
                        $nums .= $v['num'] . ",";
                    } else{
                        $nums .= $v['num'];
                    }
                }
            }
            $rt[$key]['nums'] = $nums;

        }
//        $rt = $this->where($where)->page("$pageNo,$pageSize")->order($order)->select();
//        dump(M("Order o")->getLastSql());
        return $rt;
    }

    /**
     * 获得列表（带基本查询）
     */
    public function getOrderList($pageNo = 1, $pageSize = 15, $where = array(), $order = array()) {
        $pre = C("DB_PREFIX");
        $sql1 = "select  sum(num)  from $pre" . "order_goods as og where og.orderID = o.orderID";
        $rt = M("Order o")
                        ->field("o.orderID,o.orderType,o.userID,o.orderSn,o.province,o.city,"
                        . "o.district,o.address,o.consignee,o.phone,o.zcode,"
                        . "o.logisticsNo,o.logisticsStatus,o.totalPrice,o.payPrice,o.status,"
                        . "o.isComment,o.payModeID,o.payModeName,o.payTime,o.addTime,($sql1) as numOfPack,"
                        . "u.phone userPhone,u.account userAccount,pl.paygatewayOrderNo liushui")
                        ->join("LEFT JOIN  $pre" . "user u on o.userID = u.userID LEFT JOIN $pre" . "pay_log pl on o.orderSn = pl.orderNo and pl.isError = 0 LEFT JOIN $pre" . "order_goods og on og.orderID = o.orderID")
                        ->where($where)->group('o.orderSn')->page("$pageNo,$pageSize")->order($order)->select();
        
        if($rt) {
            D("OrderGoods")->_getGoodsForOrder($rt);
        }


        foreach($rt AS $key=>$value){
            $result = M("OrderGoods")->where(array('orderID'=>$value['orderID']))->select();
            $nums = "";
            if(count($result)>1){
                foreach($result AS $k=>$v){
                    if(end($result)!= $v){
                        $nums .= $v['num'] . ",";
                    } else{
                        $nums .= $v['num'];
                    }
                }
            }
            $rt[$key]['nums'] = $nums;

        }
//        dump($pageNo);
//        dump($rt[0]);exit();
//        $rt = $this->where($where)->page("$pageNo,$pageSize")->order($order)->select();
        
//        dump(M("Order o")->getLastSql());

        return $rt;
    }

    public function getOrderListLength($where = array()) {
        $pre = C("DB_PREFIX");
        $rt = M("Order o")->where($where)
                ->join("LEFT JOIN  $pre" . "user u on o.userID = u.userID LEFT JOIN $pre" . "pay_log pl on o.orderSn = pl.orderNo and pl.isError = 0 LEFT JOIN $pre" . "order_goods og on og.orderID = o.orderID")
                ->count();
        return $rt;
//        return $this->where($where)->count();
    }

    /**
     * 添加单个信息
     * @return type
     */
    public function addOrder($data = '') {
        $rt = false;
        if ($this->create($data)) {
            $rt = $this->add();
        }
        return $rt;
    }

    /**
     * 添加订单商品
     * @return type
     */
    public function addOrderGoods($data = '') {
        $rt = false;
        if ($this->create($data)) {
            $rt = M("OrderGoods")->data($data)->add();
        }
        return $rt;
    }

    /**
     * 更新信息(注意$data一定要有主键id,若$data为空，则默认是对$_POST数组处理)
     * @return type
     */
    public function updateOrder($data = '') {
        $rt = false;
        if ($this->create($data)) {
            $rt = $this->save();
        }
        return $rt;
    }

    /**
     * 删除
     */
    public function deleteOrder($id) {
        if ($id <= 0) {
            $this->error = "无效ID";
            return false;
        }
        $data = array("isOn" => 0, "updateTime" => time());
        return $this->where("OrderID=$id")->save($data);
    }

    public function getOrderMSG($id) {
        $where = array(
            "o.orderID" => $id,
            "o.isOn" => 1
        );
        $pre = C("DB_PREFIX");
        $result = M("Order o")
                ->field("o.orderID,o.orderType,o.logisticsNo,o.logisticsStatus,o.consignee linkMan,o.phone linkPhone,o.address linkAddr,
                				o.userID,o.orderSn,o.shopID,o.totalPrice,o.payPrice,
                				o.mRemark,o.remark,o.status,o.isComment,o.payModeName,o.payTime,o.dealTime,o.addTime,"
                        . "s.shopName,s.province,s.district,s.city,s.address,"
                        . "u.phone,u.nickName")
                ->join("LEFT JOIN  $pre" . "shop s on o.shopID = s.shopID LEFT JOIN $pre" . "user u on o.userID = u.userID")
                ->where($where)
                ->find();

//        dump(M("Order o")->getLastSql());
        return $result;
    }

}
